﻿using CnBlogsClient.Global;
using CnBlogsClient.Interface;
using CnBlogsCore.Model;
using MvvmHelpers;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;

namespace CnBlogsClient.ViewModels
{
    class NewsViewModel : BaseViewModel
    {
        public ObservableRangeCollection<News> News
        {
            get;
        } = new ObservableRangeCollection<News>();

        public DateTime NextRefreshTime
        {
            get;
            set;
        }

        private int pageIndex = 1;
        private int pageSize = 20;

        private int position = 1;

        public NewsViewModel(int position)
        {
            this.position = position;
            CanLoadMore = false;
        }

        ICommand refreshCommand;
        public ICommand RefreshCommand =>
            refreshCommand ?? (refreshCommand = new Command(async ()=> 
            {
                try
                {
                    IsBusy = true;

                    NextRefreshTime = DateTime.Now.AddMinutes(15);
                    CanLoadMore = false;
                    pageIndex = 1;
                    await ExecuteRefreshCommandAsync();
                }
                catch (Exception)
                {
                    loadStatus = LoadMoreStatus.StausFail;
                }
                finally
                {
                    IsBusy = false;
                }
            }));


        LoadMoreStatus loadStatus;

        public LoadMoreStatus LoadStatus
        {
            get
            {
                return loadStatus;
            }

            set
            {
                SetProperty(ref loadStatus, value);
            }
        }

        ICommand loadMoreCommand;
        public ICommand LoadMoreCommand =>
            loadMoreCommand ?? (loadMoreCommand = new Command(async ()=>
            {
                try
                {
                    LoadStatus = LoadMoreStatus.StausLoading;
                    await ExecuteRefreshCommandAsync();
                }
                catch (Exception)
                {
                    LoadStatus = LoadMoreStatus.StausError;
                }
            }));
        async Task ExecuteRefreshCommandAsync()
        {
            List<News> addNews = new List<News>();

            await Task.Run(() =>
            {
                if (0 == position)
                {
                    addNews = DependencyService.Get<INewsService>().GetRecentNews
                     (pageIndex, pageSize);
                }
                else if (1 == position)
                {
                    addNews = DependencyService.Get<INewsService>().GetRecommendedNews
                         (pageIndex, pageSize);
                }
                else if (2 == position)
                {
                    addNews = DependencyService.Get<INewsService>().GetWeekHotNews
                     (pageIndex, pageSize);
                }
            });

            if (addNews.Count > 0)
            {
                if (pageIndex == 1 && News.Count > 0)
                {
                    News.Clear();
                }

                News.AddRange(addNews);
                pageIndex++;
                LoadStatus = LoadMoreStatus.StausDefault;
                CanLoadMore = true;
            }
            else
            {
                CanLoadMore = false;
                LoadStatus = pageIndex > 1 ? LoadMoreStatus.StausEnd : LoadMoreStatus.StausNodata;
            }
            
        }
    }
}
